#include "const.h"

void multiwildcard(var_t v, const char *path, size_t n, const char **patterns) {
	static char arraybuf[512];
	struct dirent *dp;
	size_t i;
	DIR *d;
	int e;

	if (!(d = opendir(path)))
		i_interrorf("%s: %s", path, i_errnostring());

	while((dp = readdir(d)) != (struct dirent *)0) {
		for(i=0; i<n; i++) {
			if (s_wildcard(patterns[i], dp->d_name)) {
				strcpy(arraybuf, path);
				i_strcat(arraybuf, sizeof(arraybuf), dp->d_name);

				if (isfile(arraybuf)) {
					v_array_push(v, arraybuf);
					break;
				}
			}
		}
	}

	e = errno;
	closedir(d);
	errno = e;

	if (errno)
		i_interrorf("%s: %s", path, i_errnostring());
}
